package com.school.manager.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.school.manager.common.Constants;
import com.school.manager.domaim.SysMenu;
import com.school.manager.domaim.SysPermMenu;
import com.school.manager.domaim.SysRolePerm;
import com.school.manager.entity.ResponseEntity;
import com.school.manager.entity.po.SysMenuPo;
import com.school.manager.entity.po.SysUserPo;
import com.school.manager.entity.vo.SysMenuVo;
import com.school.manager.enums.ResponseEnum;
import com.school.manager.enums.StatusEnum;
import com.school.manager.service.ISysMenuService;
import com.school.manager.service.ISysPermMenuService;
import com.school.manager.service.ISysRolePermService;
import com.school.manager.utils.ConvertUtil;
import com.school.manager.utils.MyContextHolder;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * description：菜单控制器 date：2021/10/133:18 下午
 */

@RestController
@Api(tags = "菜单")
@RequestMapping(Constants.PROGRAM_PATH + "menu")
public class SysMenuController {

	@Autowired
	private ISysMenuService sysMenuService;

	@Autowired
	private ISysRolePermService rolePermService;

	@Autowired
	private ISysPermMenuService permMenuService;

	@PostMapping("add")
	@ApiOperation("新增菜单")
	@ApiImplicitParam(name = "菜单", value = "sysMenuPo", dataTypeClass = SysMenuPo.class)
	public ResponseEntity add(@RequestBody SysMenuPo sysMenuPo) {
		return ResponseEnum.SUCCESS.success(sysMenuService.save(ConvertUtil.convertObj(sysMenuPo, SysMenu.class)));
	}

	@PostMapping("edit")
	@ApiOperation("修改菜单")
	@ApiImplicitParam(name = "菜单", value = "sysMenuPo", dataTypeClass = SysMenuPo.class)
	public ResponseEntity edit(@RequestBody SysMenuPo sysMenuPo) {
		return ResponseEnum.SUCCESS
				.success(sysMenuService.updateById(ConvertUtil.convertObj(sysMenuPo, SysMenu.class)));
	}

	@GetMapping("get/{id}")
	@ApiOperation("菜单详情")
	@ApiImplicitParam(name = "菜单主键", value = "id", dataTypeClass = String.class)
	public ResponseEntity get(@PathVariable("id") String id) {
		SysMenu sysMenu = sysMenuService.getById(id);
		return ResponseEnum.SUCCESS.success(ConvertUtil.convertObj(sysMenu, SysMenuVo.class));
	}

	@GetMapping("del/{id}")
	@ApiOperation("删除菜单")
	@ApiImplicitParam(name = "菜单主键", value = "id", dataTypeClass = String.class)
	public ResponseEntity del(@PathVariable("id") String id) {
		SysMenu sysMenu = sysMenuService.getById(id);
		sysMenu.setStatus(StatusEnum.DISABLE.getCode());
		return ResponseEnum.SUCCESS.success(sysMenuService.updateById(sysMenu));
	}

	@GetMapping("enable/{id}")
	@ApiOperation("启动菜单")
	@ApiImplicitParam(name = "菜单主键", value = "id", dataTypeClass = String.class)
	public ResponseEntity enable(@PathVariable("id") String id) {
		SysMenu sysMenu = sysMenuService.getById(id);
		sysMenu.setStatus(StatusEnum.ENABLE.getCode());
		return ResponseEnum.SUCCESS.success(sysMenuService.updateById(sysMenu));
	}

	@PostMapping("page")
	@ApiOperation("菜单分页列表")
	@ApiImplicitParam(name = "菜单Po", value = "sysMenuPo", dataTypeClass = SysMenuPo.class)
	public ResponseEntity page(@RequestBody SysMenuPo sysMenuPo) {
		sysMenuPo.setStatus(StrUtil.EMPTY);
		return ResponseEnum.SUCCESS.success(sysMenuService.page(sysMenuPo));
	}

	@GetMapping("tree")
	@ApiOperation("权限菜单树")
	public ResponseEntity tree() {
		SysUserPo userpo = MyContextHolder.getCurrentUser();
		if (ObjectUtil.isNotNull(userpo)) {
			String roleId = userpo.getRoleId();
			// 获取用户权限
			List<String> permisIds = rolePermService
					.list(new LambdaQueryWrapper<SysRolePerm>().eq(SysRolePerm::getRoleId, roleId)
							.eq(SysRolePerm::getStatus, StatusEnum.ENABLE.getCode()))
					.stream().map(SysRolePerm::getPermId).collect(Collectors.toList());
			// 获取菜单
			List<String> menuIds = permMenuService
					.list(new LambdaQueryWrapper<SysPermMenu>().in(SysPermMenu::getPermId, permisIds)
							.eq(SysPermMenu::getStatus, StatusEnum.ENABLE.getCode()))
					.stream().map(SysPermMenu::getMenuId).collect(Collectors.toList());
			List<SysMenu> menus = sysMenuService.list(new LambdaQueryWrapper<SysMenu>().in(SysMenu::getId, menuIds)
					.eq(SysMenu::getStatus, StatusEnum.ENABLE.getCode()).orderByAsc(SysMenu::getMenuOrder));
			List<SysMenuVo> menupos = ConvertUtil.convertList(menus, SysMenuVo.class);
			List<SysMenuVo> tree = ConvertUtil.convertTree(menupos, sysMenuVo -> "#".equals(sysMenuVo.getParentId()));
			return ResponseEnum.SUCCESS.success(tree);
		}
		return ResponseEnum.SUCCESS.success();
	}
	
	@GetMapping("treesetting")
	@ApiOperation("菜单配置树")
	public ResponseEntity treeSetting() {
		// 获取菜单
		List<SysMenu> menus = sysMenuService.list(new LambdaQueryWrapper<SysMenu>()
				.eq(SysMenu::getStatus, StatusEnum.ENABLE.getCode()).orderByAsc(SysMenu::getMenuOrder));
		List<SysMenuVo> menupos = ConvertUtil.convertList(menus, SysMenuVo.class);
		List<SysMenuVo> tree = ConvertUtil.convertTree(menupos, sysMenuVo -> "#".equals(sysMenuVo.getParentId()));
		return ResponseEnum.SUCCESS.success(tree);
	}
	
	@GetMapping("list/{id}")
	@ApiOperation("菜单配置树")
	public ResponseEntity list(@PathVariable("id") String id) {
		// 获取菜单
		List<String> menuIds = permMenuService
				.list(new LambdaQueryWrapper<SysPermMenu>().eq(SysPermMenu::getPermId, id)
						.eq(SysPermMenu::getStatus, StatusEnum.ENABLE.getCode()))
				.stream().map(SysPermMenu::getMenuId).collect(Collectors.toList());
		return ResponseEnum.SUCCESS.success(menuIds);
	}

}
